View Javadoc
1   package edu.jiangxin.apktoolbox.file.core;
2   
3   import java.io.BufferedReader;
4   import java.io.BufferedWriter;
5   import java.io.File;
6   import java.io.FileInputStream;
7   import java.io.FileOutputStream;
8   import java.io.InputStreamReader;
9   import java.io.OutputStreamWriter;
10  import java.util.Collection;
11  import java.util.Iterator;
12  
13  import org.apache.commons.io.FileUtils;
14  import org.apache.commons.lang3.StringUtils;
15  import org.apache.logging.log4j.LogManager;
16  import org.apache.logging.log4j.Logger;
17  
18  /**
19   * 文件编码转换的工具类.
20   * 
21   * @author jiangxin
22   * @author 2018-09-09
23   *
24   */
25  public class EncoderConvert {
26  
27      private static final String TMP_SUFFIX = ".temp";
28      private static final Logger logger = LogManager.getLogger(EncoderDetector.class.getSimpleName());
29  
30      /**
31       * 实现对文件的编码转换.
32       * 
33       * @param srcFileString 源文件文件名
34       * @param srcEncoder    源文件编码
35       * @param desFileString 目的文件文件名
36       * @param desEncoder    需要转换的编码
37       */
38      public static void encodeFile(String srcFileString, String srcEncoder, String desFileString, String desEncoder) {
39          if (srcFileString.equals(desFileString)) {
40              srcFileString = srcFileString + EncoderConvert.TMP_SUFFIX;
41              FileProcess.copyFile(desFileString, srcFileString);
42          }
43          File srcFileFile = new File(srcFileString);
44          File desFileFile = new File(desFileString);
45  
46          File parentDir = desFileFile.getParentFile();
47          if (!parentDir.exists()) {
48              boolean success = parentDir.mkdirs();
49              if (!success) {
50                  logger.error("delete srcFileFile failed");
51                  return;
52              }
53          }
54  
55          try (BufferedReader reader = new BufferedReader(
56                  new InputStreamReader(new FileInputStream(srcFileFile), srcEncoder));
57                  BufferedWriter writer = new BufferedWriter(
58                          new OutputStreamWriter(new FileOutputStream(desFileFile), desEncoder))) {
59              int ch = 0;
60              while ((ch = reader.read()) != -1) {
61                  writer.write(ch);
62              }
63          } catch (Exception e) {
64              logger.error("Exception", e);
65              return;
66          }
67  
68          if (srcFileString.equals(desFileString + EncoderConvert.TMP_SUFFIX)) {
69              boolean success = srcFileFile.delete();
70              if (!success) {
71                  logger.error("delete srcFileFile failed");
72              }
73          }
74      }
75  
76      /**
77       * 实现文件的编码转换. see {@link #encodeFile(String, String, String, String)}
78       * 
79       * @param fileString 文件名
80       * @param srcEncoder 原始编码
81       * @param desEncoder 需要转换的编码
82       */
83      public static void encodeFile(String fileString, String srcEncoder, String desEncoder) {
84          encodeFile(fileString, srcEncoder, fileString, desEncoder);
85      }
86  
87      /**
88       * 实现文件夹中特定文件的批量编码转换. see {@link #encodeFile(String, String, String, String)}
89       * 
90       * @param srcDirString 原始文件夹
91       * @param srcEncoder   原始编码
92       * @param desDirString 目的文件夹
93       * @param desEncoder   需要转换的编码
94       * @param suffix       指定需要转换的后缀
95       */
96      public static void encodeDir(String srcDirString, String srcEncoder, String desDirString, String desEncoder,
97              String suffix) {
98          // File srcDirFile = new File(srcDirString);
99          File desDirFile = new File(desDirString);
100         // 获取所有符合条件的文件
101         String[] extensions = null;
102         if (StringUtils.isNotEmpty(suffix)) {
103             extensions = suffix.split(",");
104         }
105         Collection<File> files = FileUtils.listFiles(new File(srcDirString), extensions, true);
106         Iterator<File> it = files.iterator();
107         while (it.hasNext()) {
108             File tempFile = it.next();
109             String desFileString = desDirFile.getAbsolutePath() + File.separator + tempFile.getName();
110             String srcFileString = tempFile.getAbsolutePath();
111             encodeFile(srcFileString, srcEncoder, desFileString, desEncoder);
112         }
113     }
114 
115     /**
116      * 实现文件夹中特定文件的批量编码转换. see {@link #encodeFile(String, String, String, String)}
117      * 
118      * @param srcDirString 原始文件夹
119      * @param srcEncoder   原始编码
120      * @param desDirString 目的文件夹
121      * @param desEncoder   需要转换的编码
122      */
123     public static void encodeDir(String srcDirString, String srcEncoder, String desDirString, String desEncoder) {
124         encodeDir(srcDirString, srcEncoder, desDirString, desEncoder, null);
125     }
126 }